using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class FileUtil : MonoBehaviour
{
    /// <summary>
    /// 检测文件是否存在
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static bool IsFileExist(string path)
    {
        FileInfo file = new FileInfo(path);
        return file.Exists;
    }
    
    public static void WriteFile(string path, byte[] data)
    {
        path = PathUtil.GetStandardPath(path);

        //文件夹路径
        string dir = path.Substring(0, path.LastIndexOf('/'));

        //文件夹不存在则创建
        if(!Directory.Exists(dir)) 
        {
            Directory.CreateDirectory(dir);
        }

        FileInfo file = new FileInfo(path);

        //文件夹存在则删除（覆盖式写入
        if(file.Exists) 
        {
            file.Delete();
        }

        try
        {
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) 
            {
                fs.Write(data, 0, data.Length);
                fs.Close();
            }
        }
        catch (IOException e)
        {
            Debug.LogError(e.Message);
            
        }
    }
}
